//LCR187.破冰游戏  https://leetcode.cn/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/?envType=problem-list-v2&envId=recursion

class Solution {
    public int iceBreakingGame(int num, int target) {
        //首先创建一个动态链表
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i<num;i++){
            list.add(i);
        }
        int n = list.size();
        int l = 0;
        while(n>1){

            l=(l+target-1)%n;
            list.remove(l);
            n--;
        }
        return list.get(0);
    }
}

//面试题02.04.分割链表  https://leetcode.cn/problems/partition-list-lcci/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        //首先分别生成大链表 和 小链表
        //再将两个链表进行拼接
        ListNode bh = new ListNode(0);
        ListNode sh = new ListNode(0);
        ListNode b = bh;
        ListNode s = sh;
        while(head!=null){
            if(head.val<x){
                sh.next=head;
                sh=sh.next;
            }else{
                bh.next=head;
                bh=bh.next;
            }
            head=head.next;
        }
        bh.next = null;
        sh.next = b.next;
        return s.next;

    }
}

//108.将有序数组转换为二叉搜索树  https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/?envType=problem-list-v2&envId=divide-and-conquer


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return dfs(nums,0,nums.length-1);
    }
    private TreeNode dfs (int[] nums,int left,int right){
        if(left>right){
            return null;
        }
        int mid = left+(right-left)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = dfs(nums,left,mid-1);
        root.right = dfs(nums,mid+1,right);
        return root;
    }
}